import { Component, Output, OnInit, Input, EventEmitter, OnChanges } from '@angular/core';
import {appService} from "../../../../../my-service/app.service";
import { Router } from '@angular/router';
import { UtilService } from '../../../../../my-service/util.service';
import { classService } from '../../../../../my-service/class.service';
@Component({
  selector: 'class-root',
  templateUrl: './class.component.html',
  styleUrls: ['./class.component.css'],

})
export class classComponent implements OnInit, OnChanges {
  edit = false
  classlist = [];
  FK_CanvasGuid
  img
  @Input() train: any;
  @Input() userguid: any;
  // @Output() onchange = new EventEmitter<Event>();
  @Output() ondel = new EventEmitter<Event>();
  @Output() onedit = new EventEmitter<Event>();
  constructor(
    private appservice: appService,
    private utilService: UtilService,
    private router: Router,
    private classService: classService) {
      this.FK_CanvasGuid=this.utilService.canvas().guid
      this.img=this.utilService.canvas().img
    }
  ngOnChanges($event) {
    this.update()
  }
  ngOnInit() {
    this.update()
  }
  update() {
    this.classService.ClassForUser(this.userguid).then(re => {
      let _classlist = []
      let _classlist2 = []
      for (let k = 0; k < re.data.length; k++) {
        _classlist.push({
          PK_ClassGuid: re.data[k].PK_ClassGuid,
          ClassName: re.data[k].ClassName,
          data: []
        })
      }
      _classlist.push({
        PK_ClassGuid: '000000',
        ClassName: '其他',
        data: []
      })
      for (let i = 0; i < this.train.length; i++) {
        // this.train[i].PasswordArray = this.train[i].Password.split('')
        if (this.train[i].FK_ClassGuid && this.train[i].FK_ClassGuid != 'null') {
          for (let m = 0; m < _classlist.length; m++) {
            if (this.train[i].FK_ClassGuid == _classlist[m].PK_ClassGuid) {
              _classlist[m].data.push(this.train[i])
            }
          }
        }
        else {
          _classlist[_classlist.length - 1].data.push(this.train[i])
        }
      }
      for (let j = 0; j < _classlist.length; j++) {
        if (_classlist[j].data.length > 0) {
          _classlist2.push(_classlist[j]);
        }
      }
      this.classlist = _classlist2
    })
  }
  gotodetail(path, value) {
    this.router.navigate([`${path}`, { ClassName: value }]);
  }
  goto(path) {
     this.router.navigate([`${path}`]);
  }
  del(item, j, i) {
    this.utilService.comfirm('确定要删除该训练吗？', () => {
      this.appservice.deltrain({
        PK_TrainningGuid: item.PK_TrainningGuid,
        Status: 0
      }).then(re => {
        if (re.status == 1) {
          this.classlist[j].data.splice(i, 1);
          this.ondel.emit()
          this.utilService.remind('success', '删除成功！')
        }
      })
    })
  }
  edititem(item) {
    this.onedit.emit(item)
  }
}
